package com.etime.shycourse.services.ad.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shycourse.services.ad.interfaces.AdStatisticsService;
import com.etime.shycourse.dto.AdLogDto;
import com.etime.shycourse.dto.AdStatisticDto;
import com.etime.shycourse.dao.AdStatisticMapper;
import com.etime.shycourse.pojo.AdLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * 广告统计service层接口实现类
 */
@Service
public class AdStatisticsServiceImpl implements AdStatisticsService {

    @Autowired
    private AdStatisticMapper adStatisticMapper;

    /**
     * 根据当前时间进行广告统计
     * @param
     * @return
     */
    @Override
    public AdStatisticDto countAdStatistic() {
        AdStatisticDto adStatisticDto = new AdStatisticDto();
        Date date = new Date();

        if(date!=null){
            //正在进行的广告统计
            QueryWrapper<AdLog> wrapper1= new QueryWrapper();

            wrapper1.lt("adstart",date);
            long time = date.getTime() - (1000 * 60 * 60 * 24);
            wrapper1.gt("adend",new Date(time));
            Integer progressAdNum= adStatisticMapper.selectCount(wrapper1);
            adStatisticDto.setProgressAdNum(progressAdNum);

            //已结束的广告统计
            QueryWrapper<AdLog> wrapper2= new QueryWrapper();
            wrapper2.lt("adend",new Date(time));
            Integer closedAdNum= adStatisticMapper.selectCount(wrapper2);
            adStatisticDto.setClosedAdNum(closedAdNum);

            //待投放的广告统计
            QueryWrapper<AdLog> wrapper3= new QueryWrapper();
            wrapper3.gt("adstart",date);
            Integer waitPutAdNum= adStatisticMapper.selectCount(wrapper3);
            adStatisticDto.setWaitPutAdNum(waitPutAdNum);
        }

        return adStatisticDto;
    }

    /**
     * 根据placeid进行广告收益统计
     * @param placeid
     * @return
     */
    @Override
    public AdStatisticDto countAdRevenue(Long placeid) {
        AdStatisticDto adStatisticDto = new AdStatisticDto();
        adStatisticDto.setPlaceId(placeid);
        //广告位置
        String adPlace = adStatisticMapper.selectPlace(placeid);
        adStatisticDto.setAdPlace(adPlace);

        //总广告数量
        QueryWrapper<AdLog> wrapper = new QueryWrapper();
        if (placeid!=null){
            wrapper.eq("placeid",placeid);
        }
        Integer adTotalNum = adStatisticMapper.selectCount(wrapper);
        adStatisticDto.setAdTotalNum(adTotalNum);

        //待投放的广告统计
        Date date= new Date();
        long time = date.getTime() - (1000 * 60 * 60 * 24);
        QueryWrapper<AdLog> wrapper1= new QueryWrapper();
        wrapper1.lt("adstart",date);
        wrapper1.gt("adend",new Date(time));
        wrapper1.eq("placeid",placeid);
        Integer progressAdNum= adStatisticMapper.selectCount(wrapper1);
        adStatisticDto.setProgressAdNum(progressAdNum);

        //已结束的广告统计
        QueryWrapper<AdLog> wrapper2= new QueryWrapper();
        wrapper2.lt("adend",new Date(time));
        wrapper2.eq("placeid",placeid);
        Integer closedAdNum= adStatisticMapper.selectCount(wrapper2);
        adStatisticDto.setClosedAdNum(closedAdNum);
        //待投放的广告统计
        QueryWrapper<AdLog> wrapper3= new QueryWrapper();
        wrapper3.gt("adstart",date);
        wrapper3.eq("placeid",placeid);
        Integer waitPutAdNum= adStatisticMapper.selectCount(wrapper3);
        adStatisticDto.setWaitPutAdNum(waitPutAdNum);


        //广告总收益
        Double totalAdRevenue =adStatisticMapper.selectTotalAdRevenue(placeid);
        adStatisticDto.setTotalAdRevenue(totalAdRevenue);

        return adStatisticDto;
    }

    /**
     * 根据orgid进行机构广告统计
     * @param orgid
     * @return
     */
    @Override
    public AdStatisticDto countOrgAd(Integer orgid) {
        AdStatisticDto adStatisticDto = new AdStatisticDto();
        adStatisticDto.setOrgId(orgid);
        Date date = new Date();
        long time = date.getTime() - (1000 * 60 * 60 * 24);
        //机构名称
        String orgName=adStatisticMapper.selectOrgName(orgid);
        adStatisticDto.setOrgName(orgName);
        //投放广告总数量
        Integer adTotalNum = adStatisticMapper.selectAdTotalNum(orgid);
        adStatisticDto.setAdTotalNum(adTotalNum);
        //已结束的广告数量
        Integer closedAdNum = adStatisticMapper.selectClosedAdNum(orgid, new Date(time));
        adStatisticDto.setClosedAdNum(closedAdNum);
        //正在进行的广告数量
        Integer progressAdNum = adStatisticMapper.selectProgressAdNum(orgid, new Date(time));
        adStatisticDto.setProgressAdNum(progressAdNum);
        //等待投放的广告数量
        Integer waitPutAdNum = adStatisticMapper.selectWaitPutAdNum(orgid, date);
        adStatisticDto.setWaitPutAdNum(waitPutAdNum);
        //总金额
        Double adRevenue = adStatisticMapper.selectAdRevenue(orgid);
        adStatisticDto.setTotalAdRevenue(adRevenue);

        return adStatisticDto;
    }


    /**
     * 根据广告状态查询广告列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<AdLogDto> selectAdList(Long pageNum, Long pageSize,Integer adstate) {
        Page<AdLogDto> page =new Page<>(pageNum,pageSize);
        return adStatisticMapper.selectAdList(page,adstate);
    }
    /**
     * 根据广告id和广告状态查询广告列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<AdLogDto> selectAdListByPlaceId(Long pageNum, Long pageSize, Integer adstate, Long placeid) {
        Page<AdLogDto> page =new Page<>(pageNum,pageSize);
        return adStatisticMapper.selectAdListByPlaceId(page,adstate,placeid);
    }

    /**
     * 根据广告id和广告状态查询广告列表
     * @param pageNum
     * @param pageSize
     * @param adstate
     * @param orgid
     * @return
     */
    @Override
    public Page<AdLogDto> selectAdListByOrgId(Long pageNum, Long pageSize, Integer adstate, Integer orgid) {
        Page<AdLogDto> page =new Page<>(pageNum,pageSize);
        return adStatisticMapper.selectAdListByOrgId(page,adstate,orgid);
    }
}
